package com.lmb.todo.service.impl;

import com.lmb.todo.dao.GroupDao;
import com.lmb.todo.pojo.Group;
import com.lmb.todo.service.GroupService;
import com.lmb.todo.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lmb
 */
@Service
public class GroupServiceImpl implements GroupService {

    @Autowired
    private GroupDao groupDao;

    @Override
    public List<Group> getAllByUserId(Integer userId) {
        if (userId < 0) {
            return null;
        }
        return groupDao.getAllByUserId(userId);
    }

    @Override
    public Integer update(Group group) {
        if (group != null) {
            group.setUpdateTime(DateUtil.getCurrentDateStr());
            return groupDao.update(group);
        }
        return null;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer delete(Integer id) {
        if (id > 0) {
            deleteTodo(id);
            return groupDao.delete(id);
        }
        return null;
    }

    @Override
    public Integer move(Integer folderId, Integer id) throws Exception {
        if (id > 0) {
            return groupDao.move(folderId, id);
        }
        return 0;
    }

    @Override
    public Integer add(Group group) {
        if (group != null) {
            group.setCreateTime(DateUtil.getCurrentDateStr());
            group.setCount(0);
            return groupDao.add(group);
        }
        return 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer deleteTodo(Integer groupId) {
        if (groupId > 0) {
            return groupDao.deleteTodo(groupId);
        }
        return 0;
    }

    @Override
    public Integer count(Integer id, Integer count, String type) {
        if (id > 0) {
            return groupDao.count(id, count, type);
        }
        return 0;
    }

    @Override
    public List<Integer> getGroupIdsByUserId(Integer userId) {
        if (userId > 0) {
            return groupDao.getGroupIdsByUserId(userId);
        }
        return new ArrayList<>();
    }
}
